
#import "GalleryLampHotspotView.h"

#define kHotspotSize CGSizeMake(80, 114)

@interface GalleryLampHotspotView ()
@property (weak, nonatomic) IBOutlet UIImageView *hotspotInnerImage;
@property (weak, nonatomic) IBOutlet UIImageView *hotspotOuterImage;
@property (weak, nonatomic) IBOutlet UIButton *infoButton;
@property (weak, nonatomic) IBOutlet UIView *hotspotView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonCenterXConstraint;

@property (strong, nonatomic) GalleryLampTapHandler infoTapHandler;
@property (assign, nonatomic) BOOL isAnimating;

@end


@implementation GalleryLampHotspotView

- (instancetype)initWithCenterPoint:(CGPoint)point
                     infoTapHandler:(GalleryLampTapHandler)infoTapHandler {
    self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                          owner:self
                                        options:nil] objectAtIndex:0];
    _infoTapHandler = infoTapHandler;
    
    self.frame = CGRectMake(round(point.x - kHotspotSize.width / 2), round(point.y - kHotspotSize.height / 2), 0, 0);
    [self sizeToFit];
    
    self.autoresizingMask = UIViewAutoresizingNone;
    
    _hotspotInnerImage.alpha = 0;
    _hotspotOuterImage.alpha = 0;

    _hotspotView.alpha = 1;
    _infoButton.alpha = 0;
    
    return self;
}

-(CGSize)sizeThatFits:(CGSize)size {
    return kHotspotSize;
}

-(void)sizeToFit {
    CGRect frame = self.frame;
    frame.size = [self sizeThatFits:CGSizeZero];
    self.frame = frame;
}

-(void)setButtonInLeftCorner:(BOOL)buttonInLeftCorner {
    _buttonInLeftCorner = buttonInLeftCorner;
    CGFloat offset = ABS(_buttonCenterXConstraint.constant);
    _buttonCenterXConstraint.constant = buttonInLeftCorner ? offset : -offset;
}

#pragma mark - Animation

#define kShowHideDuration 0.3

- (void)showHotspot {
    [UIView animateWithDuration:kShowHideDuration animations:^{
        self.hotspotView.alpha = 1;
        self.infoButton.alpha = 0;
    }];
}

- (void)showInfoButton {
    [UIView animateWithDuration:kShowHideDuration animations:^{
        self.infoButton.alpha = 1;
        self.hotspotView.alpha = 0;
    }];
}

#define kAnimationDuration 1

#define kInnerImageAlpha 0.7
#define kOuterImageAlphaStart 0.2
#define kOuterImageAlphaEnd 0.1
#define kOuterImageScaleStart 1.0
#define kOuterImageScaleEnd 0.8

- (void)startAnimation {
    _hotspotInnerImage.alpha = kInnerImageAlpha;
    _hotspotOuterImage.alpha = kOuterImageAlphaStart;
    _hotspotOuterImage.transform = CGAffineTransformMakeScale(kOuterImageScaleStart, kOuterImageScaleStart);
    [UIView animateWithDuration:kAnimationDuration
                          delay:0.0
                        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.hotspotOuterImage.alpha = kOuterImageAlphaEnd;
                         _hotspotOuterImage.transform = CGAffineTransformMakeScale(kOuterImageScaleEnd, kOuterImageScaleEnd);
                     }
                     completion:nil
     ];
}

#pragma mark - Actions

- (IBAction)infoButtonPressed {
    self.infoTapHandler();
}

@end
